Micron Document
Livres et Wikis | Archives | Info


JavaScript syntax
part 13/31 Β· 107.4 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
One can use the object and array declaration literals to quickly create arrays that are associative, multidimensional, or both. (Technically, JavaScript does not support multidimensional arrays, but one can mimic them with arrays-of-arrays.)

const cats = [{color: "brown", size: "large"},
{color: "black", size: "small"}];
cats[0]["size"]; // results in "large"
const dogs = {rover: {color: "brown", size: "large"},
spot: {color: "black", size: "small"}};
dogs["spot"]["size"]; // results in "small"
dogs.rover.color; // results in "brown"

Date

A Date object stores a signed millisecond count with zero representing 1970-01-01 00:00:00 UT and a range of Β±108 days. There are several ways of providing arguments to the Date constructor. Note that months are zero-based.

new Date(); // create a new Date instance representing the current time/date.
new Date(2010, 2, 1); // create a new Date instance representing 2010-Mar-01 00:00:00
new Date(2010, 2, 1, 14, 25, 30); // create a new Date instance representing 2010-Mar-01 14:25:30
new Date("2010-3-1 14:25:30"); // create a new Date instance from a String.

Methods to extract fields are provided, as well as a useful toString:

const d = new Date(2010, 2, 1, 14, 25, 30); // 2010-Mar-01 14:25:30;
// Displays '2010-3-1 14:25:30':
console.log(d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' '
+ d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds());
// Built-in toString returns something like 'Mon 1 March, 2010 14:25:30 GMT-0500 (EST)':
console.log(d);

Error

Custom error messages can be created using the Error class:

throw new Error("Something went wrong.");

These can be caught by try...catch...finally blocks as described in the section on exception handling.

Math

The Math object contains various math-related constants (for example, Ο€) and functions (for example, cosine). (Note that the Math object has no constructor, unlike Array or Date. All its methods are "static", that is "class" methods.) All the trigonometric functions use angles expressed in radians, not degrees or grads.

| Property | Returned value rounded to 5 digits | Description |
|---|---|---|
| Math.E | 2.7183 | e : Natural logarithm base |
| Math.LN2 | 0.69315 | Natural logarithm of 2 |
| Math.LN10 | 2.3026 | Natural logarithm of 10 |
| Math.LOG2E | 1.4427 | Logarithm to the base 2 of e |
| Math.LOG10E | 0.43429 | Logarithm to the base 10 of e |
| Math.PI | 3.14159 | Ο€ : circumference/diameter of a circle |
| Math.SQRT1_2 | 0.70711 | Square root of Β½ |
| Math.SQRT2 | 1.4142 | Square root of 2 |

| Example | Returned value rounded to 5 digits |
|---|---|
| Math.abs(-2.3) | 2.3 |
| Math.acos(Math.SQRT1_2) | 0.78540 rad = 45Β° |
| Math.asin(Math.SQRT1_2) | 0.78540 rad = 45Β° |
| Math.atan(1) | 0.78540 rad = 45Β° |
| Math.atan2(-3.7, -3.7) | βˆ’2.3562 rad = βˆ’135Β° |
| Math.ceil(1.1) | 2 |
| Math.cos(Math.PI/4) | 0.70711 |
| Math.exp(1) | 2.7183 |
| Math.floor(1.9) | 1 |
| Math.log(Math.E) | 1 |
| Math.max(1, -2) | 1 |
| Math.min(1, -2) | βˆ’2 |
| Math.pow(-3, 2) | 9 |
| Math.random() | e.g. 0.17068 |
| Math.round(1.5) | 2 |
| Math.sin(Math.PI/4) | 0.70711 |
| Math.sqrt(49) | 7 |
| Math.tan(Math.PI/4) | 1 |

| Example | Description |
|---|---|
| Math.abs(-2.3) | Absolute value |
| Math.acos(Math.SQRT1_2) | Arccosine |
| Math.asin(Math.SQRT1_2) | Arcsine |
| Math.atan(1) | Half circle arctangent ( ⁠ βˆ’ Ο€ / 2 {\d… |
| Math.atan2(-3.7, -3.7) | Whole circle arctangent ( ⁠ βˆ’ Ο€ {\disp… |
| Math.ceil(1.1) | Ceiling: round up to smallest integer β‰₯… |
| Math.cos(Math.PI/4) | Cosine |
| Math.exp(1) | Exponential function : e raised to this… |
| Math.floor(1.9) | Floor: round down to largest integer ≀… |
| Math.log(Math.E) | Natural logarithm, base e |
| Math.max(1, -2) | Maximum: (x > y) ? x : y |
| Math.min(1, -2) | Minimum: (x < y) ? x : y |
| Math.pow(-3, 2) | Exponentiation (raised to the power of)… |
| Math.random() | Pseudorandom number between 0 (inclusiv… |
| Math.round(1.5) | Round to the nearest integer; half frac… |
| Math.sin(Math.PI/4) | Sine |
| Math.sqrt(49) | Square root |
| Math.tan(Math.PI/4) | Tangent |

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────